package com.example.queue;

import java.util.ArrayList;
import java.util.List;

/**
 * 模拟队列
 */
public class Queue {
    public static void main(String[] args) {
        MyQueue<Integer> queue = new MyQueue<>();
        queue.push(1);
        queue.push(2);
        Integer peek = queue.peek();// 返回 1
        System.out.print("peek result: " + (peek == null ? "is null" : peek));
        Integer pop = queue.pop();// 返回 1
        System.out.print("\npop result: " + (pop == null ? "is null" : pop));
        boolean empty = queue.empty();// 返回 false
        System.out.print("\nempty result: " + empty);
//        MyQueue<Integer> queue = new MyQueue<>();
//        queue.push(1);
//        queue.push(2);
//        queue.push(3);
//        System.out.print("\nqueue: " + queue.toString());
//        System.out.print("\npop result: " + queue.pop());
//        System.out.print("\nqueue: " + queue.toString());
//        queue.push(4);
//        queue.push(5);
//        System.out.print("\nqueue: " + queue.toString());
//        while (!queue.empty()) {
//            System.out.print("\npop result: " + queue.pop());
//            System.out.print("\nqueue: " + queue.toString());
//        }
    }
    
    public static class MyQueue<T> {
        private MyStack<T> temp;
        private MyStack<T> stack;

        public MyQueue() {
            temp = new MyStack<>();
            stack = new MyStack<>();
        }

        public void push(T x) {
            if (stack.empty()) {
                temp.push(x);
                return;
            }
            while (!stack.empty()) {
                temp.push(stack.pop());
            }
            temp.push(x);
        }

        public T pop() {
            if (temp.empty()) {
                return stack.pop();
            }
            while (!temp.empty()) {
                stack.push(temp.pop());
            }
            return stack.pop();
        }

        public T peek() {
            if (temp.empty()) {
                return stack.peek();
            }
            while (!temp.empty()) {
                stack.push(temp.pop());
            }
            return stack.peek();
        }

        public boolean empty() {
            return temp.empty() && stack.empty();
        }

        @Override
        public String toString() {
            if (!stack.empty()) {
                while (!stack.empty()) {
                    temp.push(stack.pop());
                }
            }
            return "[" + temp.toString() + "]";
        }
    }

    public static class MyStack<T> {
        private List<T> list;

        public MyStack() {
            list = new ArrayList<>();
        }

        //push to top
        public void push(T x) {
            list.add(x);
        }

        // pop from top
        public T pop() {
            if (list == null || list.size() == 0) {
                return null;
            }
            int index = list.size() - 1;
            T t = list.get(index);
            list.remove(index);
            return t;
        }

        //peek from top
        public T peek() {
            if (list == null || list.size() == 0) {
                return null;
            }
            return list.get(list.size() - 1);
        }

        //is empty
        public boolean empty() {
            return list == null || list.size() == 0;
        }

        //size
        public int size() {
            if (empty()) {
                return 0;
            }
            return list.size();
        }

        @Override
        public String toString() {
            return "[" + list.toString() + "]";
        }
    }

}